//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace Preflight.ServiceModel.Push
{
    using System;
    using System.Threading;
    using System.Timers;
    using Timer = System.Timers.Timer;

    internal class ContinuousPreflightRunner : IContinuousPreflightRunner
    {
        private readonly IPreflightRunner preflightRunner;
        private readonly Timer checkTimer = new Timer();
        private readonly object timerLock = new object();

        public ContinuousPreflightRunner(IPreflightRunner preflightRunner, TimeSpan frequency)
        {
            this.preflightRunner = preflightRunner;
            this.checkTimer.Interval = frequency.TotalMilliseconds;
            this.checkTimer.Elapsed += this.TimerElapsed;
        }

        public event Action<Exception> Error;

        public bool IsRunning
        {
            get
            {
                return this.checkTimer.Enabled;
            }
        }

        public void Start()
        {
            if (!this.IsRunning)
            {
                this.checkTimer.Start();
            }
        }

        public void Stop()
        {
            if (this.IsRunning)
            {
                this.checkTimer.Stop();
            }
        }

        public void Dispose()
        {
            this.checkTimer.Dispose();
        }

        private void TimerElapsed(object sender, ElapsedEventArgs e)
        {
            if (!Monitor.TryEnter(this.timerLock))
            {
                return;
            }

            try
            {
                this.preflightRunner.PushPreflightCheckResults();
            }
            catch (Exception exception)
            {
                if (this.Error != null)
                {
                    this.Error(exception);
                }
                else
                {
                    Log.Error("Error execution Preflight Check Push. Error: {0}", exception);
                }
            }
            finally
            {
                Monitor.Exit(this.timerLock);
            }
        }
    }
}
